package com.suguowen.sblog.common;

import com.suguowen.sblog.bean.User;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 缓存已登录对象，单例
 * @Author suguowen
 * @date 2019/4/11 11:36
 */
public class MySpringContext {

    private static MySpringContext context = null;

    private static ConcurrentHashMap<String,User> userCache = null;

    private MySpringContext() {
    }

    public static MySpringContext getInstance(){
        if (context == null){
            synchronized (MySpringContext.class){
                if (context == null){
                    context = new MySpringContext();
                    userCache = new ConcurrentHashMap<>();
                }
            }
        }
        return context;
    }

    public static User getUser(String sessionId){
        return userCache.get(sessionId);
    }
    // 这里不能用putIfAbsent，业务逻辑不认可，当同一个sessionId选择重新开户登录，sessionId不变，登录另一个号，这里需要覆盖
    public static void putUser(String sessionid, User user){
        userCache.put(sessionid,user);
    }

}
